$schemes = {};
$schemes["trackpad"] = { sensitivity: 14, effectiveRange: 35 };
$schemes["trackpoint"] = { sensitivity: 12, effectiveRange: 35 };
$schemes["regular"] = { sensitivity: 1, effectiveRange: 35 };

if (localStorage["schemes.custom"] == null)
{
    $schemes["custom"] = { sensitivity: 10, effectiveRange: 35 };
    localStorage["schemes.custom"] = JSON.stringify($schemes["custom"]);
}
else
{
    $schemes["custom"] = eval("(" + localStorage["schemes.custom"] + ")");
}




$settings = null;
if (localStorage["settings"] == null)
{
    loadScheme("custom");
    $settings.enabled = true;
    $settings.scheme = "custom";
    saveSettings();
}
else
{
    $settings = eval("(" + localStorage["settings"] + ")");
}



chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse)
    {
        if (request.action == "getSettings")
        {
            sendResponse($settings);
        }
    }
);







function onstart()
{
    $("#chkEnabled").attr("checked", $settings.enabled);
    $("#chkEnabled").iphoneStyle({ onChange: _chkEnabled_onchange });
    
    
    $("#spSensitivity").change(_spSensitivity_onchange);
    $("#spEffectiveRange").change(_spEffectiveRange_onchange);
    
    
    _chkEnabled_onchange(null, $settings.enabled);
    selectTab($settings.scheme, false);
}


function selectTab(p_name, p_save)
{
    $(".selected").removeClass("selected");
    $("#" + p_name).addClass("selected");
    
    applyScheme(p_name);
    
    if (p_save != false)
    {
        saveSettings();
    }
}






function applyScheme(p_name)
{
    loadScheme(p_name);
    
    $("#spSensitivity").val($settings.sensitivity);
    $("#spEffectiveRange").val($settings.effectiveRange);    
}

function saveSettings()
{
    localStorage["settings"] = JSON.stringify($settings);
    chrome.extension.sendRequest({ action: "settingsChanged", newSettings: $settings });
}

function loadScheme(p_name)
{
    if ($settings == null)
    {
        $settings = {};
    }
    $settings.scheme = p_name;
    $settings.sensitivity = $schemes[p_name].sensitivity;
    $settings.effectiveRange = $schemes[p_name].effectiveRange;
}




function _chkEnabled_onchange(e, value)
{
    $settings.enabled = value;
    saveSettings();
    
    if (value)
    {
        $("#selector").fadeIn();
        $("#optionPage").animate({ height: 550 }, "fast");
    }
    else
    {
        $("#selector").fadeOut();
        $("#optionPage").animate({ height: 120 }, "fast");
    }
}

function _spSensitivity_onchange(e)
{
    if ($settings.scheme != "custom")
    {
        $settings.scheme = "custom";
        $(".selected").removeClass("selected");
        $("#custom").addClass("selected");
    }
    $settings.sensitivity = parseInt($("#spSensitivity").val());
    localStorage["schemes.custom"] = JSON.stringify({ sensitivity: $settings.sensitivity, effectiveRange: $settings.effectiveRange });
    saveSettings();
}

function _spEffectiveRange_onchange(e)
{
    if ($settings.scheme != "custom")
    {
        $settings.scheme = "custom";
        $(".selected").removeClass("selected");
        $("#custom").addClass("selected");
    }
    $settings.effectiveRange = parseInt($("#spEffectiveRange").val());
    localStorage["schemes.custom"] = JSON.stringify({ sensitivity: $settings.sensitivity, effectiveRange: $settings.effectiveRange });
    saveSettings();
}